package com.moive.news.tqnewsapplication.weight;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.moive.news.tqnewsapplication.R;

/**
 * Created by Administrator on 2017/12/11.
 */

public class SwitchView extends LinearLayout implements View.OnClickListener {
    private static final int FLAG_MOVE_TRUE = 1;
    private static final int FLAG_MOVE_FALSE = 2;
    private static final int HANDLE_LAYOUT_CURSOR = 100;

    private Context context;
    private RelativeLayout sv_container;
    private ImageButton iv_switch_cursor;
    private TextView switch_text_true;
    private TextView switch_text_false;

    private String textTrue;
    private String textFalse;

    private boolean isChecked = true;
    private boolean checkedChange = false;
    private OnCheckedChangeListener onCheckedChangeListener;

    private int margin = 1;
    private int bg_left;
    private int bg_right;
    private int cursor_left;
    private int cursor_top;
    private int cursor_right;
    private int cursor_bottom;
    private LayoutParams lp;

    private Animation animation;
    private int currentFlag = FLAG_MOVE_TRUE;

    public String getTextTrue() {
        return textTrue;
    }

    public void setTextTrue(String textTrue) {
        this.textTrue = textTrue;

        switch_text_true.setText(textTrue);

    }

    public String getTextFalse() {
        return textFalse;
    }

    public void setTextFalse(String textFalse) {
        this.textFalse = textFalse;
        switch_text_false.setText(textFalse);
    }

    public SwitchView(Context context) {
        super(context);
        this.context = context;
        initView();
    }

    public SwitchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initView();


        TypedArray styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.SwitchView);
        this.setTextTrue(styledAttrs.getString(R.styleable.SwitchView_textTrue));
        this.setTextFalse(styledAttrs.getString(R.styleable.SwitchView_textFalse));

//		int c_h=(int) styledAttrs.getDimension(R.styleable.SwitchView_container_Hight, 120);
//		int c_w=(int)styledAttrs.getDimension(R.styleable.SwitchView_container_Width, 660);
//		int iv_h=(int) styledAttrs.getDimension(R.styleable.SwitchView_cursor_Hight,108);
//		int iv_w=(int)styledAttrs.getDimension(R.styleable.SwitchView_cursor_Width,332);

//		int c_h=(int) styledAttrs.getDimension(R.styleable.SwitchView_container_Hight, 60);
//		int c_w=(int)styledAttrs.getDimension(R.styleable.SwitchView_container_Width, 330);
//		int iv_h=(int) styledAttrs.getDimension(R.styleable.SwitchView_cursor_Hight,54);
//		int iv_w=(int)styledAttrs.getDimension(R.styleable.SwitchView_cursor_Width,166);


//		sv_container.getLayoutParams().height=c_h;
//		sv_container.getLayoutParams().width=c_w;
        sv_container.getLayoutParams().height=LayoutParams.WRAP_CONTENT;
        sv_container.getLayoutParams().width=LayoutParams.WRAP_CONTENT;
        Drawable drawable1=styledAttrs.getDrawable(R.styleable.SwitchView_container_Background);
        if(drawable1!=null)
            //sv_container.setBackground(drawable1);

            sv_container.invalidate();
//		iv_switch_cursor.getLayoutParams().height=iv_h;
//		iv_switch_cursor.getLayoutParams().width=iv_w;
        iv_switch_cursor.getLayoutParams().height=LayoutParams.WRAP_CONTENT;
        iv_switch_cursor.getLayoutParams().width=LayoutParams.WRAP_CONTENT;
        Drawable drawable2=styledAttrs.getDrawable(R.styleable.SwitchView_cursor_Background);
        if(drawable1!=null)
//		iv_switch_cursor.setBackground(drawable2);
            iv_switch_cursor.invalidate();

//		iv_switch_cursor.setLayoutParams(new LayoutParams(iv_w,iv_h));
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);

        bg_left = sv_container.getLeft();
        bg_right = sv_container.getRight();
        cursor_left = iv_switch_cursor.getLeft();
        cursor_top = iv_switch_cursor.getTop();
        cursor_right = iv_switch_cursor.getRight();
        cursor_bottom = iv_switch_cursor.getBottom();
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case HANDLE_LAYOUT_CURSOR:
                    iv_switch_cursor.layout(cursor_left, cursor_top, cursor_right,
                            cursor_bottom);
                    break;
            }
        }
    };

    public void onClick(View v) {

        if (v == this) {
            changeChecked(!isChecked);
        }
    }


    private void initView() {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.switch_view, this);
        view.setOnClickListener(this);
        sv_container = (RelativeLayout) view.findViewById(R.id.sv_container);

        switch_text_true = (TextView) view.findViewById(R.id.switch_text_true);
        switch_text_false = (TextView) view
                .findViewById(R.id.switch_text_false);
        changeTextColor();
        iv_switch_cursor = (ImageButton) view.findViewById(R.id.iv_switch_cursor);
        iv_switch_cursor.setClickable(false);
        iv_switch_cursor.setOnTouchListener(new OnTouchListener() {
            int lastX;

            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        lastX = (int) event.getRawX();

                        cursor_left = v.getLeft();
                        cursor_top = v.getTop();
                        cursor_right = v.getRight();
                        cursor_bottom = v.getBottom();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        int dx = (int) event.getRawX() - lastX;

                        cursor_left = v.getLeft() + dx;
                        cursor_right = v.getRight() + dx;


                        if (cursor_left <= bg_left + margin) {
                            cursor_left = bg_left + margin;
                            cursor_right = cursor_left + v.getWidth();
                        }
                        if (cursor_right >= bg_right - margin) {
                            cursor_right = bg_right - margin;
                            cursor_left = cursor_right - v.getWidth();
                        }
                        v.layout(cursor_left, cursor_top, cursor_right,
                                cursor_bottom);

                        lastX = (int) event.getRawX();
                        break;
                    case MotionEvent.ACTION_UP:
                        calculateIscheck();
                        break;
                }
                return true;
            }
        });
    }


    private  void calculateIscheck() {
        float center = (float) ((bg_right - bg_left) / 2.0);
        float cursor_center = (float) ((cursor_right - cursor_left) / 2.0);
        if (cursor_left + cursor_center <= center) {
            changeChecked(true);
        } else {
            changeChecked(false);
        }
    }

    private  void  changeChecked(boolean isChecked) {
        if (this.isChecked != isChecked) {
            checkedChange = true;
        } else {
            checkedChange = false;
        }
        if (isChecked) {
            currentFlag = FLAG_MOVE_TRUE;
        } else {
            currentFlag = FLAG_MOVE_FALSE;
        }
        cursorMove();
    }


    private void cursorMove() {

        animation = null;
        final int toX;
        if (currentFlag == FLAG_MOVE_TRUE) {
            toX = cursor_left - bg_left - margin;
            animation = new TranslateAnimation(0, -toX, 0, 0);
        } else {
            toX = bg_right - margin - cursor_right;
            animation = new TranslateAnimation(0, toX, 0, 0);
        }
        animation.setDuration(100);
        animation.setInterpolator(new LinearInterpolator());
        animation.setAnimationListener(new Animation.AnimationListener() {
            public void onAnimationStart(Animation animation) {

            }

            public void onAnimationRepeat(Animation animation) {

            }

            public void onAnimationEnd(Animation animation) {

                if (currentFlag == FLAG_MOVE_TRUE) {
                    cursor_left -= toX;
                    cursor_right = cursor_left + iv_switch_cursor.getWidth();
                } else {
                    cursor_right = bg_right - margin;
                    cursor_left = cursor_right - iv_switch_cursor.getWidth();
                }

                mHandler.sendEmptyMessageDelayed(HANDLE_LAYOUT_CURSOR, 5);

                if (checkedChange) {
                    isChecked = !isChecked;
                    if (onCheckedChangeListener != null) {
                        onCheckedChangeListener.onCheckedChanged(isChecked);
                    }
                    changeTextColor();

                }
            }
        });
        iv_switch_cursor.startAnimation(animation);
    }

    /**
     * 设置字体颜色
     */
    private void changeTextColor() {
        if (isChecked) {
            switch_text_true.setTextColor(Color.parseColor("#ff0000"));
            switch_text_false.setTextColor(Color.GRAY);

        } else {
            switch_text_true.setTextColor(Color.GRAY);
            switch_text_false.setTextColor(Color.parseColor("#ff0000"));

        }
    }


    private void layoutCursor() {
        if (isChecked) {
            cursor_left = bg_left + margin;
            cursor_right = bg_left + margin + iv_switch_cursor.getWidth();
        } else {
            cursor_left = bg_right - margin - iv_switch_cursor.getWidth();
            cursor_right = bg_right - margin;
        }
        iv_switch_cursor.layout(cursor_left, cursor_top, cursor_right,
                cursor_bottom);
    }


    public interface OnCheckedChangeListener {
        void onCheckedChanged(boolean isChecked);
    }

    public boolean isChecked() {
        return isChecked;
    }

    public void setChecked(boolean isChecked) {
        if (this.isChecked != isChecked) {
            this.isChecked = isChecked;
            if (onCheckedChangeListener != null) {
                onCheckedChangeListener.onCheckedChanged(isChecked);
            }
            layoutCursor();
        }
    }

    public void setOnCheckedChangeListener(
            OnCheckedChangeListener onCheckedChangeListener) {
        this.onCheckedChangeListener = onCheckedChangeListener;
    }

}

